package com.liuwei.msmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuwei.msmall.common.utils.PageUtils;
import com.liuwei.msmall.common.utils.Query;
import com.liuwei.msmall.product.dao.ProductAttrValueDao;
import com.liuwei.msmall.product.entity.AttrEntity;
import com.liuwei.msmall.product.entity.ProductAttrValueEntity;
import com.liuwei.msmall.product.service.AttrService;
import com.liuwei.msmall.product.service.ProductAttrValueService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Resource
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProductAttrValueEntity> getSearchAttrBySpuId(Long spuId) {

        List<ProductAttrValueEntity> productAttrValueEntities = this.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));


        //1.拿到这个spu下的所有规格参数ids
        List<Long> attrids = productAttrValueEntities.stream().map(item -> {
            return item.getAttrId();
        }).collect(Collectors.toList());

        //2.在拿到设置成可检索的规格属性
        List<AttrEntity> searchAttrs= attrService.getSearchAttrByIds(attrids);
        List<Long> searchAttrIds = searchAttrs.stream().map(attr -> attr.getAttrId()).collect(Collectors.toList());
        List<ProductAttrValueEntity> collect = productAttrValueEntities.stream().filter(i -> searchAttrIds.contains(i.getAttrId())).collect(Collectors.toList());

        return collect;
    }

}